package com.atguigu.dga.governance.assessor.spec;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.governance.assessor.Assessor;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: TableColumnComment
 * Package: com.atguigu.dga.governance.assessor.spec
 * Description:
 *
 * @Author:
 * @Create: 2024/3/18 11:09
 * @Version: 1.0
 */
@Component("TABLE_COLUMN_COMMENT")
public class TableColumnCommentAssessor extends Assessor {
    @Override
    public void checkProblem(AssessParam assessParam, GovernanceAssessDetail governanceAssessDetail) {
        // 表字段是否有备注
        String colNameJson = assessParam.getTableMetaInfo().getColNameJson();
        List<JSONObject> colNameJsonObjectList = JSON.parseArray(colNameJson, JSONObject.class);

        ArrayList<String> missCommentColName = new ArrayList<>();
        for (JSONObject colJsonObj : colNameJsonObjectList) {
            String colComment = colJsonObj.getString("comment");
            if(colComment == null || colComment.trim().isEmpty()) {
                missCommentColName.add(colJsonObj.getString("name"));
            }
        }

        if (missCommentColName.size() > 0) {
            governanceAssessDetail.setAssessScore(BigDecimal.valueOf(
                    (colNameJsonObjectList.size() - missCommentColName.size()) * 10L / colNameJsonObjectList.size()));
            governanceAssessDetail.setAssessProblem("有字段缺失备注");
            governanceAssessDetail.setAssessComment("没有备注的字段：" + missCommentColName);
        }
    }
}
